<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "{{%goods}}".
 *
 * @property integer $goods_id
 * @property string $goods_name
 * @property string $goods_price
 * @property integer $goods_thumbnail
 * @property string $goods_desc
 * @property integer $created_at
 * @property integer $updated_at
 *
 * @property File $file
 * @property GoodsExtra $goodsExtra
 */
class Goods extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%goods}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['goods_price', 'goods_thumbnail', 'goods_desc', 'goods_name'], 'required'],
            [['goods_price'], 'number'],
            [['goods_thumbnail', 'created_at', 'updated_at'], 'integer'],
            [['goods_desc'], 'string'],
            [['goods_name'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'goods_id' => '排序',
            'goods_name' => '诊疗项目',
            'goods_price' => '价格',
            'goods_thumbnail' => '缩略图',
            'goods_desc' => '详细描述',
            'created_at' => '创建时间',
            'updated_at' => '修改时间',
        ];
    }
    
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ]
            ]
        ];
    }
    
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getFile()
    {
        return $this->hasOne(File::className(), ['file_id' => 'goods_thumbnail']);
    }
    
    public function getGoodsExtra()
    {
        return $this->hasOne(GoodsExtra::className(), ['goods_id' => 'goods_id'])->onCondition(['package_id' => Yii::$app->user->identity->vip_type]);
    }
    
    public static function findList()
    {
        return static::find()->asArray()->all();
    }
    
    public static function findById($id)
    {
        return static::findOne(['goods_id' => $id]);
    }
    
    
}
